package com.jbm.jbmsupplier.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.jbm.jbmsupplier.R;
import com.jbm.jbmsupplier.UpdateVersion;
import com.jbm.jbmsupplier.activity.loadhomepage.factoryhome.LoadHomeFactory;
import com.jbm.jbmsupplier.presenters.SplasActivitys;
import com.jbm.jbmsupplier.shared.SharedPreferActivity;
import com.jbm.jbmsupplier.utlis.ExitActivity;
import com.jbm.jbmsupplier.views.SplsViews;

import butterknife.ButterKnife;
import butterknife.InjectView;


/**
 * Created by 家帮忙 on 2017/4/10.
 * 李征
 */

public class SplasActivity extends AllActivityApp implements SplsViews {
    @InjectView(R.id.splas_image)
    ImageView splasImage;
    private SplasActivitys splasActivitys;
    private LoadHomeFactory loadhomefactory;
    private SharedPreferActivity sharedPreferActivity;

    @Override
    public int setContentView() {
        return R.layout.splasactivity_lz;
    }
    @Override
    public void initIalizeView() {
        ExitActivity.getInstance().addActivity(this);
        ButterKnife.inject(this);

        splasActivitys=new SplasActivitys(this,this);
         loadhomefactory = new LoadHomeFactory();//使用工厂模式判断是那个管理员
        sharedPreferActivity=new SharedPreferActivity(this,true);
        //执行启动页动画
        initanim();
    }
    private void initanim() {
       splasImage.setAnimation( AnimationUtils.loadAnimation(this,R.anim.splasanim));
       splasActivitys.setInitIal();
    }
    @Override
    public void setInvisionit() {//调用检测版本号接口
        initvision();
    }
    private void initvision() {//获取当前版本号
        PackageManager packages=getPackageManager();
        try {
            PackageInfo viname = packages.getPackageInfo(getPackageName(), 0);
            String visionname = viname.versionName;
            splasActivitys.setSplaVison(visionname);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }
    private String urls;
    private boolean islo;
    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void setSucc(String url,boolean Islogin) {//成功
          urls=url;
        islo=Islogin;
        if (Build.VERSION.SDK_INT >= 23) {
        int i = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (i == 0) {
            initSkipActivity(Islogin);
           new UpdateVersion(SplasActivity.this, url);
           } else {
           requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
         }}else{
            initSkipActivity(Islogin);
            new UpdateVersion(SplasActivity.this, url);
        }
    }

    @Override
    public void setError(String error,boolean Islogin) {//失败
        initSkipActivity(Islogin);
    }

    @Override
    public void setfinish() {
        finish();//必须更新
    }

    private void initSkipActivity(boolean IsLogin){
        if (IsLogin==false){
            startActivity(new Intent(this,LoginActivity.class));
        }else{
                loadhomefactory.getLoagHomePage("2").LoadNewHome(this);//第二个代表管理
        }
        finish();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if(grantResults[0]==0){
            new UpdateVersion(SplasActivity.this, urls);
            initSkipActivity(islo);
        }else{
            finish();
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        splasActivitys.setonDestroy();//将广播停止

    }
}
